package com.wyvoid.forum.dao.hibernate;

import com.wyvoid.core.persistent.Pagination;
import com.wyvoid.core.persistent.hibernate.HibernateDAO;
import com.wyvoid.core.persistent.hibernate.HibernatePage;
import com.wyvoid.forum.dao.INoteDAO;
import com.wyvoid.forum.domain.Note;

import java.util.List;

/**
 * 公告信息Hibernate数据处理实现
 */
public class NoteDAO extends HibernateDAO<Note> implements INoteDAO {

    /**
     * 查找日志信息
     * <ol>
     * <li>forumId为-1时,取全部信息</li>
     * <li>forumId为0时,首页信息</li>
     * </ol>
     *
     * @param forumId    论坛编号
     * @param pagination 分页对象
     * @return QueryResult 公告集合
     */
    public HibernatePage<Note> findNotes(final int forumId, final Pagination pagination) {
        if (forumId == -1) {
            String hql = "from Announcement as announcement order by announcement.dateCreated desc";
            return doQuery(hql,true,pagination);
        } else {
            String hql = "from Announcement as announcement where forumId=? order by announcement.dateCreated desc";
            return doQuery(hql,true,pagination,forumId);
        }

    }

    /**
     * 根据论坛编号取得公告信息
     *
     * @param forumId 论坛编号
     * @return List 公告集合
     */
    public List<Note> findNotes(int forumId) {
        if (forumId == -1) {
            String hql = "from Announcement as announcement order by announcement.dateCreated desc";
            return super.doQueryList(hql, true);
        } else {
            String hql = "from Announcement as announcement where forumId=? order by announcement.dateCreated desc";
            return super.doQueryList(hql, true, forumId);
        }
    }
}